package com.mtable.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mtable.entity.MMenu;
import com.mtable.entity.MUser;
import com.mtable.service.MMenuService;
import com.mtable.service.MUserService;
import com.mtable.service.impl.MUserServiceImpl;

@Controller
public class MUserAction {
	
	public static final String USER_SESS = "user_sess";
	
	@Autowired
	private MUserService userService;
	@Autowired
	private MMenuService menuService;
	
	@RequestMapping("/muser/login")
	public String login(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String username = request.getParameter("userName");
		String password = request.getParameter("password");
		
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw = response.getWriter();
		int result = userService.login(username, password);
		if(result == MUserServiceImpl.ALL_RIGHT){
			request.getSession().setAttribute(MUserAction.USER_SESS, username);
			pw.print(result);
		}else{
			pw.print(result);
		}
		return null;
	}
	@RequestMapping("/muser/main")
	public String main(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String menuJsonStr = menuService.getAllMenuItemJsonStr();
		request.setAttribute("menuJsonStr", menuJsonStr);
		return "main";
	}
	
	@RequestMapping("/notlogined")
	public String login1(HttpServletRequest request,HttpServletResponse response)throws Exception{
		return "notlogined";
	}
	
	@RequestMapping("/userManage_list")
	public String userList(HttpServletRequest request,HttpServletResponse response){
		
		//将结果转为 json字符串
//		JSONArray jarry = new JSONArray();
//		JSONObject jobj;
//		for(MUser us : userList){
//			jobj = new JSONObject(us);
//			jarry.put(jobj);
//		}
//		String userListJson = jarry.toString();
//		response.addHeader("Content-Range", "items ");
//		
//		request.setAttribute("userList", userList);
//		request.setAttribute("userListJson", userListJson);
		//client Range header: Range: items=0-24
		//server side return header: Content-Range: items 0-24/66
		/**The resulting request to the server would be:
			/FooObject/?foo=value1&sortBy=+foo,-bar
		If sortParam is not set, the sort value is appended without a key-value pair, surrounded by "sort()":
			/FooObject/?foo=value1&sort(+foo,-bar)
		*/
//		if(queryRange == null){
//			userList = userService.findAll();
//		}else{
//			userList = userService.findAll();
//		}
		return "user/userList";
	}
	@RequestMapping("/muser/user")
	public String getUserList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		//调用 service 传入request 得到结果
		List<MUser> userList = this.userService.query(request,response);
		String jsonStr = this.userService.generateJsonArray(userList);
		response.getWriter().print(jsonStr);
		return null;
	}
	@RequestMapping(value = "/muser/user/{userId}", method = RequestMethod.GET)
	public String getUser(@PathVariable String userId) {
		return null;
	  // implementation omitted...
	}
	@RequestMapping(value = "/muser/user/{userId}", method = RequestMethod.DELETE)
	public String deleteUser(@PathVariable String userId) {
		return null;
	  // implementation omitted...
	}
	//修改
	@RequestMapping(value = "/muser/user/{userId}", method = RequestMethod.PUT)
	public String putUser(@PathVariable String userId) {
		return null;
	  // implementation omitted...
	}
	
}
